/*
 * ShopAllGoodsPresenter.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.shopdetail.allgoods;

import android.os.Bundle;

import com.yidiandao.business.R;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.model.IShopModel;
import com.yidiandao.business.data.recyclertype.DoubleGoods;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/** 店铺详情-全部商品-Presenter */
public class ShopAllGoodsPresenter implements ShopAllGoodsContract.Presenter {

    private ShopAllGoodsContract.View mAllGoodsView;

    private IShopModel mShopModel;

    private String mSupplierId;

    private boolean firstIn = true;

    private boolean isGridStyle;

    private boolean mEmptyResult;

    private boolean mHasMoreData;

    private List<Goods> mGoodsList;

    private List<DoubleGoods> mGoodsGridList;

    public ShopAllGoodsPresenter(ShopAllGoodsContract.View view, IShopModel shopModel) {
        mAllGoodsView = view;
        mShopModel = shopModel;

        mAllGoodsView.setPresenter(this);
    }

    @Override
    public void start() {
    }

    @Override
    public void bundleSupplierId(Bundle bundle) {
        mSupplierId = bundle.getString(Intents.EXTRA_SUPPLIER_ID);
    }

    @Override
    public void loadShopNewestGoods(final boolean isRefreshing) {
        if (isRefreshing) {
            if (!firstIn) {
                mAllGoodsView.showLoadingDialog();
            }
        }
        mShopModel.loadShopNewestGoods(
                isRefreshing,
                mSupplierId,
                mAllGoodsView.getPagerTag(),
                new IShopModel.OnLoadShopGoodsListener() {
                    @Override
                    public void onSuccess(
                            List<Goods> goodsList,
                            List<DoubleGoods> goodsGridList,
                            boolean emptyResult,
                            boolean hasMoreData) {

                        if (goodsGridList.size() == 0) {
                            mAllGoodsView.showToast(UIUtils.getString(R.string.shop_no_goods_hint));
                        }

                        mGoodsList = goodsList;
                        mGoodsGridList = goodsGridList;
                        mEmptyResult = emptyResult;
                        mHasMoreData = hasMoreData;
                        refreshStyleUI(isGridStyle);
                    }

                    @Override
                    public void onFailed() {
                        mAllGoodsView.loadMoreError();
                    }

                    @Override
                    public void onFinal() {
                        firstIn = false;
                        mAllGoodsView.hideLoadingDialog();
                    }
                });
    }

    @Override
    public void loadShopPriceGoods(boolean isRefreshing, boolean sortUp) {

        if (isRefreshing) {
            mAllGoodsView.showLoadingDialog();
        }
        mShopModel.loadShopPriceGoods(
                isRefreshing,
                sortUp,
                mSupplierId,
                mAllGoodsView.getPagerTag(),
                new IShopModel.OnLoadShopGoodsListener() {
                    @Override
                    public void onSuccess(
                            List<Goods> goodsList,
                            List<DoubleGoods> goodsGridList,
                            boolean emptyResult,
                            boolean hasMoreData) {
                        if (goodsGridList.size() == 0) {
                            mAllGoodsView.showToast(UIUtils.getString(R.string.shop_no_goods_hint));
                        }
                        mGoodsList = goodsList;
                        mGoodsGridList = goodsGridList;
                        mEmptyResult = emptyResult;
                        mHasMoreData = hasMoreData;
                        refreshStyleUI(isGridStyle);
                    }

                    @Override
                    public void onFailed() {
                        mAllGoodsView.loadMoreError();
                    }

                    @Override
                    public void onFinal() {
                        mAllGoodsView.hideLoadingDialog();
                    }
                });
    }

    @Override
    public void loadShopSalesGoods(boolean isRefreshing) {

        if (isRefreshing) {
            mAllGoodsView.showLoadingDialog();
        }
        mShopModel.loadShopSalesGoods(
                isRefreshing,
                mSupplierId,
                mAllGoodsView.getPagerTag(),
                new IShopModel.OnLoadShopGoodsListener() {
                    @Override
                    public void onSuccess(
                            List<Goods> goodsList,
                            List<DoubleGoods> goodsGridList,
                            boolean emptyResult,
                            boolean hasMoreData) {
                        if (goodsGridList.size() == 0) {
                            mAllGoodsView.showToast(UIUtils.getString(R.string.shop_no_goods_hint));
                        }
                        mGoodsList = goodsList;
                        mGoodsGridList = goodsGridList;
                        mEmptyResult = emptyResult;
                        mHasMoreData = hasMoreData;
                        refreshStyleUI(isGridStyle);
                    }

                    @Override
                    public void onFailed() {
                        mAllGoodsView.loadMoreError();
                    }

                    @Override
                    public void onFinal() {
                        mAllGoodsView.hideLoadingDialog();
                    }
                });
    }

    @Override
    public void changeStyle(boolean isGridStyle) {
        this.isGridStyle = isGridStyle;
        refreshStyleUI(isGridStyle);
    }

    private void refreshStyleUI(boolean isGridStyle) {
        if (isGridStyle) {
            // 当前是网格状
            mAllGoodsView.setGoodsList(mGoodsList);
        } else {
            mAllGoodsView.setGoodsGrid(mGoodsGridList);
        }
        mAllGoodsView.loadMoreFinish(mEmptyResult, mHasMoreData);
    }
}
